package cn._tedu.eguser1.eguser1.base.exception;

import cn._tedu.eguser1.eguser1.base.response.Result;
import cn._tedu.eguser1.eguser1.base.response.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author tune_the last
 * @date 2025/11/17 - 10:46
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
//    @ExceptionHandler(IllegalArgumentException.class)
    public Result IllegalArgumentException(IllegalArgumentException exception){
        log.error("参数错误出现力：" +exception.getMessage());
        return Result.fail(exception.getMessage());
    }

//    @ExceptionHandler(RuntimeException.class)
        public Result RuntimeException(RuntimeException exception){
        log.error("父类异常出现力：" +exception.getMessage());
        return Result.fail(exception.getMessage());
    }
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result MethodArgumentNotValidException(MethodArgumentNotValidException exception){
        String message = exception.getFieldError().getDefaultMessage();
        log.error("MethodArgumentNotValidException：" +message);
        return Result.fail(StatusCode.VALIDATION_FAIL,message);
    }
//    @ExceptionHandler(Throwable.class)
        public Result throwAbleException(Throwable exception){
        log.error("全局异常出现力：" +exception.getMessage());
        return Result.fail(exception.getMessage());
    }

}
